<template>
<div class="app-container">
  <!--工具栏-->
  <div class="head-container">
    <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
    <crudOperation :permission="permission"/>
    <!--表单组件-->
    <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <!--  <el-form-item label="id" prop="id">
          <el-input v-model="form.id" style="width: 370px;"/>
        </el-form-item>-->
        <el-form-item label="风格名称"  prop="name">
          <el-input v-model="form.name" style="width: 370px;" maxlength="20"/>
        </el-form-item>
        <el-form-item label="封面" prop="coverPath">

          <el-upload
            :show-file-list="false"

            :on-error="handleError"
            action="api_ws/api/Contract/AttachmentUpload"
            :http-request="imageChange"
            class="avatar-uploader el_upload_legt">
            <img v-if="form.coverPath" :src="form.coverPath" title="封面" height="180px" width="180px">
            <i v-else class="el-icon-plus avatar-uploader-icon" title="封面" ></i>
          </el-upload>

        </el-form-item>

        <el-form-item label="类型"  prop="typeId">
          <el-select v-model="form.typeId" clearable placeholder="请选择类型" class="filter-item"
                     style="width: 150px">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="价格"  prop="price">
          <el-input v-model="form.price" style="width: 370px;"/>
        </el-form-item>
        <el-form-item label="定金" prop="deposit">
          <el-input v-model="form.deposit" style="width: 370px;"/>
        </el-form-item>
        <el-form-item label="套餐详情" prop="serviceDetails">
          <el-input v-model="form.serviceDetails" style="width: 370px;"/>
        </el-form-item>

        <el-form-item label="是否推荐价格">
          <input type="radio" value="0" v-model="form.priceRecommend"> 否&nbsp;&nbsp;
          <input type="radio" value="1" v-model="form.priceRecommend">是&nbsp;&nbsp;
        </el-form-item>
        <el-form-item label="是否推荐作品">
          <input type="radio" value="0" v-model="form.workRecommend"> 否&nbsp;&nbsp;
          <input type="radio" value="1" v-model="form.workRecommend">是&nbsp;&nbsp;
        </el-form-item>
        <el-form-item label="是否开启">
          <input type="radio" value="0" v-model="form.isOpen"> 否&nbsp;&nbsp;
          <input type="radio" value="1" v-model="form.isOpen">是&nbsp;&nbsp;
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="name" label="风格名称"/>
      <el-table-column prop="typeName" label="类型名称"/>
        <el-table-column prop="price" label="价格"/>
        <el-table-column prop="deposit" label="定金"/>
        <el-table-column prop="serviceDetails" label="套餐详情"/>
        <el-table-column prop="priceRecommend" label="是否推荐价格" :formatter="formatterpriceRecommend" />
        <el-table-column prop="workRecommend" label="是否推荐作品" :formatter="formatterworkRecommend"/>
        <el-table-column prop="isOpen" label="是否开启" :formatter="formatterisOpen"/>
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','weWeddingStyle:edit','weWeddingStyle:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination/>
  </div>
</div>
</template>

<script>
  import crudWeWeddingStyle from '@/api/weWeddingStyle'
  import CRUD, {presenter, header, form, crud} from '@crud/crud'
  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation'
  import udOperation from '@crud/UD.operation'
  import pagination from '@crud/Pagination'
  import { getToken } from '@/utils/auth'
  import axios from 'axios'
  import request from '@/utils/request'
  const defaultForm = {
  id:
  '',
  coverPath:
    '',

  filePathId: '',

typeId:
  '',
name:
  '',
price:
  '',
deposit:
  '',
serviceDetails:
  '',
priceRecommend:
  '0',
workRecommend:
  '0',
isOpen:
  '0'  }
  export default {
    name: 'WeWeddingStyle',
    components: {pagination, crudOperation, rrOperation, udOperation},
    mixins: [presenter(), header(), form(defaultForm), crud()],
    cruds() {
      return CRUD({ title: '婚礼风格', url: 'api/weWeddingStyle', sort: 'id,desc', crudMethod: { ...crudWeWeddingStyle }, optShow: {
          add: true,
          edit: true,
          del: true,
          download: false,
          reset: true
        }})
    },
    data() {
      return {
        typeList: [],//类型集合
        permission: {
          add: ['admin', 'weWeddingStyle:add'],
          edit: ['admin', 'weWeddingStyle:edit'],
          del: ['admin', 'weWeddingStyle:del']
        },
        rules: {
          name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
          coverPath: [{required: true, message: '封面不能为空', trigger: 'blur'}],
          typeId: [{required: true, message: '类型不能为空', trigger: 'blur'}],
          price: [{required: true, message: '价格不能为空', trigger: 'blur'}],
          deposit: [{required: true, message: '定金不能为空', trigger: 'blur'}],
          serviceDetails: [{required: true, message: '套餐详情不能为空', trigger: 'blur'}]

        }  }
    },
    created() {
      this.getTypeList()
    },
    methods: {
      formatterpriceRecommend: function (row, column) {

        if (row.priceRecommend == 1) {

          return '推荐';
        } else {
          return '不推荐';
        }
      },
      formatterworkRecommend: function (row, column) {

        if (row.workRecommend == 1) {
          return '推荐';
        } else {
          return '不推荐';
        }
      },
      formatterisOpen: function (row, column) {

        if (row.isOpen == 1) {
          return '已开启';
        } else {
          return '未开启';
        }
      },
      // 获取数据前设置好接口地址
      [CRUD.HOOK.beforeRefresh]() {
        return true
      },
      getTypeList() {
        var _this = this;
        request({
          url: "api/weWeddingStyle/getTypeList",
          method: "post",
        }).then(response => {
          if (null != response && '' != response) {
            _this.typeList = response.typeLists
          }
        })
      },
      imageChange(param,type){
         var _this=this
        let formData = new FormData()
        formData.append('file', param.file)
        let config = {
          headers: { 'Authorization': getToken() }
        }; //添加请求头
        axios.post('/api/weWeddingStyle/uploadFile', formData,config).then(res => {
          _this.form.coverPath=res.data.filePath;
          _this.form.filePathId=res.data.filePathId;

        });
      }


    }
  }
</script>
<style >
  .ql-editor.ql-blank, .ql-editor {
    height: 300px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 170px;
    line-height: 178px;
    text-align: center;
  }

</style>
